package com.suoluo.zenadmin.service.crawler;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

import javax.annotation.Resource;

import com.suoluo.zenadmin.common.config.SystemConfigHolder;
import com.suoluo.zenadmin.service.crawler.resolver.CrawlerResovlerTask;
import com.suoluo.zenadmin.service.hotel.HotelService;
import com.suoluo.zenadmin.vo.hotel.HotelReviewVO;

public class CrawlerService {

	private static CrawlerService service = new CrawlerService();

	@Resource(name="hotelService")
	private HotelService hotelService;

	private final ExecutorService crawlerExecutor = Executors
			.newFixedThreadPool(5, new ThreadFactory() {

				private final AtomicInteger threadIndex = new AtomicInteger(1);

				@Override
				public Thread newThread(Runnable r) {
					Thread t = new Thread(r, "crawler-thread-"
							+ threadIndex.getAndIncrement());
					return t;
				}
			});

	private final ScheduledExecutorService crawlerScheduleExecutor = Executors
			.newSingleThreadScheduledExecutor(new ThreadFactory() {

				@Override
				public Thread newThread(Runnable r) {
					Thread t = new Thread(r, "crawler-schedule-thread");
					return t;
				}
			});

	private CrawlerService() {
	}

	public static CrawlerService getInstance() {
		return service;
	}

	private CrawlerResovlerTask wrapperTask(HotelReviewVO review) {
		CrawlerResovlerTask task = new CrawlerResovlerTask();
		task.setHotelId(review.getHotelId());
		task.setReviewId(review.getReviewId());
		task.setReviewUrl(review.getReviewUrl());
		task.setReviewType(review.getReviewType());
		return task;
	}

	public void init() {
//		// 1.加载被中断的任务,加载状态为等待中，抓取中的任务
//		int stats = HotelReviewBean.CRAWLER_SUCCESS;
//		List<HotelReviewVO> hotelReviewList = hotelService
//				.findInterruptReview(stats);
//		for (HotelReviewVO review : hotelReviewList) {
//			this.submitTask(wrapperTask(review));
//		}
//
//		// 2.每天凌晨1点启动定时任务
//		Calendar cal = Calendar.getInstance();
//
//		// cal.add(Calendar.DAY_OF_MONTH, 1);
//		// cal.set(Ca, value)
//		cal.set(Calendar.HOUR_OF_DAY, 1);
//		cal.set(Calendar.MINUTE, 0);
//		cal.set(Calendar.SECOND, 0);
//		long initialDelay = cal.getTimeInMillis() - System.currentTimeMillis();
//		crawlerScheduleExecutor.scheduleAtFixedRate(new CrawlerScheduleTask(),
//				initialDelay, SystemConfigHolder.getCrawlerScheduleMills(),
//				TimeUnit.MILLISECONDS);
//		service = this;
	}

	void submitTask(CrawlerResovlerTask task) {
		task.submitBefore();
		crawlerExecutor.submit(task);
	}

	public void crawlerHotelReview(HotelReviewVO review) {
		submitTask(wrapperTask(review));
	}

	public void destory() {
		crawlerScheduleExecutor.shutdownNow();
		crawlerExecutor.shutdownNow();
	}

	private class CrawlerScheduleTask implements Runnable {
		@Override
		public void run() {
			// 查询抓取时间超过一周的评论
			List<HotelReviewVO> hotelReviewList = hotelService
					.findExpireReview(SystemConfigHolder
							.getCrawlerReviewHoldDays());
			for (HotelReviewVO review : hotelReviewList) {
				submitTask(wrapperTask(review));
			}
		}
	}

	// public static void main(String []args){
	// Calendar cal = Calendar.getInstance();
	// cal.add(Calendar.DAY_OF_MONTH, 1);
	// cal.set(Calendar.HOUR_OF_DAY, 1);
	// cal.set(Calendar.MINUTE, 0);
	// cal.set(Calendar.SECOND, 0);
	// System.out.println(cal.getTime().toLocaleString());
	// System.out.println(cal.getTimeInMillis());
	// System.out.println(TimeUnit.DAYS.toMillis(1));
	// System.out.println(Integer.valueOf("  23 "));
	// }
}
